﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IServices;
using System.ComponentModel;
using DataAccess;
using System.Data.Linq.Mapping;
using System.Data.Linq;//注意要引用using System.Data.Linq才行
using System.Configuration;
using System.Reflection;
using UnitHelper;
using System.Linq.Expressions;
using System.Linq.Dynamic;

namespace Services
{
    /// <summary>
    ///通用service 
    /// </summary>
    public class CommonService : ICommonService
    {
        #region 查询

        /// <summary>
        /// 获取所有数据
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<T> GetAllList<T>() where T : class
        {
            using (var db = new NewDataContext())
            {
                db.ObjectTrackingEnabled = false;
                return db.GetTable<T>().ToList();
            }
        }

        /// <summary>
        ///根据KeyId 获取实体数据 
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public virtual T GetInfoByKeyId<T>(string keyId) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.ObjectTrackingEnabled = false;
                return db.GetTable<T>().SingleOrDefault(LinqExpressHelper.GetLinqLambda<T>("KeyId", keyId));
            }
        }


        /// <summary>
        ///根据表达式获取实体数据
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public T GetEntity<T>(Expression<Func<T, bool>> expression) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.ObjectTrackingEnabled = false;
                return db.GetTable<T>().FirstOrDefault(expression);
            }
        }


        /// <summary>
        ///根据表达式获取列表数据
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<T> GetListByWhere<T>(Expression<Func<T, bool>> expression) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.ObjectTrackingEnabled = false;
                return db.GetTable<T>().Where(expression).ToList<T>();
            }
        }


        /// <summary>
        /// 根据sql获取数据
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public virtual List<T> GetListBySql<T>(string sql) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.ObjectTrackingEnabled = false;
                return db.GetTable<T>().Where(sql).ToList(); 
            }
        }

        


        /// <summary>
        ///根据表达式获取分页数据
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public List<T> GetListByPage<T>(Condition condition, Expression<Func<T, bool>> expression) where T : class
        {
            using (var db = new NewDataContext())
            {
                List<T> list = null;
                db.ObjectTrackingEnabled = false;

                if (condition != null && !condition.LoadAllRecords)
                {
                    //查询指定数量
                    list = db.GetTable<T>().Where(expression).
                        Skip(condition.PageSize * condition.CurrentPageIndex).//跳过的条数
                        Take(condition.PageSize).//查询的数量
                        ToList<T>();
                }
                else
                {
                    //查询全部
                    list = db.GetTable<T>().Where(expression).ToList<T>();
                }
                return list;
            }
        }

        #endregion

        #region 插入

        /// <summary>
        /// 添加新实例
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Insert)]
        public virtual void AddEntity<T>(T entity) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.GetTable<T>().InsertOnSubmit(entity);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 添加新实例列表
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Insert)]
        public void AddEntity<T>(IEnumerable<T> entityList) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.GetTable<T>().InsertAllOnSubmit(entityList);
                db.SubmitChanges();
            }
        }

        #endregion


        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="KeyId">标识</param>
        [DataObjectMethod(DataObjectMethodType.Delete)]
        public virtual void DeleteEntity<T>(string KeyId) where T : class
        {
            using (var db = new NewDataContext())
            {
                T entity = db.GetTable<T>().SingleOrDefault<T>(LinqExpressHelper.GetLinqLambda<T>("KeyId", KeyId));
                if (entity == null)
                    return;
                //DataBackDeleteEntity(appEntity);
                db.GetTable<T>().DeleteOnSubmit(entity);
                db.SubmitChanges();
            }
        }


        /// <summary>
        ///根据表达式获取列表 
        /// </summary> 
        public virtual void DeleteEntityByWhere<T>(Expression<Func<T, bool>> expression) where T : class
        {
            using (var db = new NewDataContext())
            {
                var list = db.GetTable<T>().Where(expression).ToList<T>();
                if (list == null || list.Count() == 0)
                    return;
                //this.DataBackDeleteEntity<T>(list); 
                db.GetTable<T>().DeleteAllOnSubmit<T>(list);
                db.SubmitChanges();
            }
        }

        #endregion

        #region 更新

        /// <summary>
        /// 更新实例
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Update)]
        public virtual void UpdateEntity<T>(T entity) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.GetTable<T>().Attach(entity, true);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 更新实例
        /// </summary> 
        [DataObjectMethod(DataObjectMethodType.Update)]
        public void UpdateEntity<T>(IEnumerable<T> entity) where T : class
        {
            using (var db = new NewDataContext())
            {
                db.GetTable<T>().AttachAll(entity, true);
                db.SubmitChanges();
            }
        }

        #endregion


    }



}
